package net.sf.jmuserver.gs.database;

import com.mchange.v2.c3p0.DataSources;
import java.sql.*;
import javax.sql.*;

import net.sf.jmuserver.gs.GameServerConfig;

public class MuDataBaseFactory {
    private static MuDataBaseFactory _instance;
    private String                   Driver, Url, Login, Password;
    private DataSource               _source;
    private Statement                _syst;

    public MuDataBaseFactory() {
        try {

            // Properties serverSettings = new Properties();
            // InputStream is = getClass().getResourceAsStream("/data/server.cfg");
            // serverSettings.load(is);
            Driver = "com.mysql.jdbc.Driver";
            Url    = "jdbc:mysql://dbserver:3306/muonline";
            Login    = "dbserver";
            Password = "ghjcnj";
            Class.forName("com.mysql.jdbc.Driver").newInstance();

            DataSource unpooled = DataSources.unpooledDataSource(Url, Login, Password);

            _source = DataSources.pooledDataSource(unpooled);

            /* Test the connection */

            _source.getConnection().close();
        } catch (SQLException e) {
            System.out.println("blad polaczenia do bd1");
            e.printStackTrace();
        } catch (InstantiationException e) {
            System.out.println("blad polaczenia do bd2");
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            System.out.println("blad polaczenia do bd3");

            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            System.out.println("blad polaczenia do bd4");

            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static MuDataBaseFactory getInstance() throws SQLException {
        if (_instance == null) {
            _instance = new MuDataBaseFactory();
        }

        return _instance;
    }

    public Connection getConnection() throws SQLException {
        return _source.getConnection();
    }

    public Statement getSyst() {
        return _syst;
    }
}

